/*
* 输入框的验证和提示
*/

var v_patrnInt = /^[0-9]\d*$/;
var v_patrnNInt = /^[1-9]\d*$/;
var v_patrnEnnum = /[\W]/;
var v_patrnNumber = /^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,3})?$/;
var v_patrnPNumber = /^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,6})?$/;
var v_patrnMobile = /^(?:1[3458]\d)-?\d{5}(\d{3}|\*{3})$/;
var v_patrnTel = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;

var _isie6_ = false;
if ($.browser.msie && $.browser.version < 7) {
	_isie6_ = true;
}

var namespace_arrys = ['pf', 'im', 'os', 'p','cts'];
 //当前URL是否存在struts命名空间
function isIncludeNameSpace(field){
 	for(var i=0; i<namespace_arrys.length; i++){
 		if(field.indexOf('\/' + namespace_arrys[i] +  '\/') != -1){
 			return true;
 		}
 	}
 	return false;
}

//初始化提示框
function initValidateTipsDiv(obj, objId) {
	obj = $(obj);
	if(!$("#" + objId)[0]){
        var tagHtml = "";
        tagHtml += '<div class="tips_div" id="'+objId+'">';
        var prefix = '';
	   	if(!isIncludeNameSpace(document.URL)){
	   		prefix = 'cts/';
	   	}
        tagHtml += '<img src="'+prefix+'images/tips_error.gif"/>';
        tagHtml += '</div>';
        $("body").append(tagHtml);
        $("#" + objId).click(function(){
        	obj.focus();
        })
	}
	$("#" + objId).hide();
	/**
	//focus时出显示提示
	var value = obj.val();
	if(value != '' && startValidateTips(obj)){
		return;
	}
	showValidateTipsDiv(obj, getValidateTipsMsg(obj))
	*/
}

//定位提示框位置
function showValidateTipsDiv(obj, msg, specWidth) {
	obj = $(obj);
	var theLogicObj = $("#tipsDiv_" + obj.attr('id'));
	if($.trim(theLogicObj.find("img").attr('title')) == ''){
		var top = obj.offset().top;
		var left = obj.offset().left;
		
		if (_isie6_) {
			var fixtop = top + (obj.height() - theLogicObj.height()) / 2 + 4;
			var fixleft = left + (obj.width() - theLogicObj.width()) + 2 + 4;
		}else{
			var fixtop = top + (obj.height() - theLogicObj.height()) / 2 + 2;
			var fixleft = left + (obj.width() - theLogicObj.width()) + 2;
		}
				
		theLogicObj.css({"top":fixtop, "left":fixleft});
		theLogicObj.find("img").attr('title', msg);
	}
	theLogicObj.show();
}

//取提示框文字
function getValidateTipsMsg(obj){
	var rules = obj.attr('v_tips');
	var arrs = rules.split(',');
	var msg = '';
	for(var i=0; i<arrs.length; i++){
		if(arrs[i] == 'notnull'){
			msg += '不能为空,';
		}else if(arrs[i] == 'number'){
			msg += '请输入数字,';
		}else if(arrs[i] == 'pnumber'){
			msg += '请输入正数,';
		}else if(arrs[i] == 'ni'){
			msg += '请输入非负整数,';
		}else if(arrs[i] == 'i'){
			msg += '请输入正整数,';
		}else if(arrs[i] == 'ennum'){
			msg += '请使用英文a-z及数字1-9组合,限10位字符,';
		}else if(arrs[i].indexOf('TO') != -1){
			var min = parseInt(arrs[i].substring(0, arrs[i].indexOf('TO')));
			var max = parseInt(arrs[i].substring(arrs[i].indexOf('TO') + 2));
			msg += '（'+min+'至'+max+'之间）,';
		}else if(arrs[i] == 'cntrNO'){
			msg += '柜号必须由4个英文字母加7个数字组成,';
		}else if(arrs[i] == 'mobile'){
			msg += '不是正确的手机号码,';
		}else if(arrs[i] == 'tel'){
			msg += '不是正确的电话号码,';
		}else if(arrs[i] == 'mobileOrTel'){
			msg += '不是正确的电话或手机号码,';
		}
	}
	return  msg.charAt(msg.length - 1) == ',' ? msg.substring(0, msg.length - 1) : msg;
}

//验证输入
//ni = nonnegative integer （非负整数）
//i 正整数
//TO  有数字范围
//nullable 允许为空
//notnull 不允许为空
//number 正确的数值
//ennum
//cntrNO 柜号4个大写字母加7位数字
//mobile 手机号码
//tel 电话
//mobileOrTel 手机号码或者电话
//return true/false
function startValidateTips(obj){
	var rules = obj.attr('v_tips');
	if(!rules && rules == ''){
		return;
	}
	if(rules.indexOf('notblur') >= 0){
		initValidateTipsDiv($(obj), 'tipsDiv_' + $(obj).attr('id'));
	}
	var arrs = rules.split(',');
	var value = obj.val();
	var pass = true;
	for(var i=0; i<arrs.length; i++){
		if(arrs[i] == 'nullable'){
			if(value == ''){
				pass = true;
				break;
			}
		}else if(arrs[i] == 'notnull'){
			if($.trim(value) == ''){
				pass = false;
				break;
			}
		}else if(arrs[i] == 'ennum'){
			if(!v_patrnEnnum.exec(value)){
				pass = false;
				continue;
			}
		}else if(arrs[i] == 'number'){
			if(!v_patrnNumber.exec(value)){
				pass = false;
				continue;
			}
		}else if(arrs[i] == 'pnumber'){
			if(!v_patrnPNumber.exec(value)){
				pass = false;
				continue;
			}
		}else if(arrs[i] == 'ni'){
			if(!v_patrnInt.exec(value) || parseInt(value) < 0){
				pass = false;
				continue;
			}
		}else if(arrs[i] == 'i'){
			if(!v_patrnNInt.exec(value) || parseInt(value) <= 0){
				pass = false;
				continue;
			}
		}else if(arrs[i].indexOf('TO') != -1){
			var min = parseInt(arrs[i].substring(0, arrs[i].indexOf('TO')));
			var max = parseInt(arrs[i].substring(arrs[i].indexOf('TO') + 2));
			if(parseInt(value) < min || parseInt(value) > max){
				pass = false;
				continue;
			}
		}else if(arrs[i] == 'cntrNO'){
			value = value.trim().toUpperCase();
			obj.val(value);
			var regResult1 = !(new RegExp("^[A-Za-z]+$").test(value.substring(0, 4)));
			var regResult2 = !(new RegExp("^[0-9]*$").test(value.substring(4, value.length)));
			if(value.length != 11 || regResult1 || regResult2){
				pass = false;
				continue;
			}
		}else if(arrs[i] == 'mobile'){
			if(!v_patrnMobile.exec(value.trim())){
				pass = false;
				continue;
			}
		}else if(arrs[i] == 'tel'){
			if(!v_patrnTel.exec(value.trim())){
				pass = false;
				continue;
			}
		}else if(arrs[i] == 'mobileOrTel'){
			if(!v_patrnMobile.exec(value.trim()) && !v_patrnTel.exec(value.trim())){
				pass = false;
				continue;
			}
		}
	}
	if(!pass){
		showValidateTipsDiv(obj, getValidateTipsMsg(obj))
		obj.attr('v_ispass', 0);
		return false;
	}else{
		$("#tipsDiv_" + obj.attr('id')).hide();
		obj.attr('v_ispass', 1);
		return true;
	}
}

//验证
//notblur 失焦不马上验证
$.fn.validateTips = function(){
	var rules = $(this).attr('v_tips');
	if(rules && rules != '' && rules.indexOf('notblur') == -1){
		$(this).focus(function(){
			initValidateTipsDiv($(this), 'tipsDiv_' + $(this).attr('id'));
		}).blur(function(){
			startValidateTips($(this));
		})
		this[0].startValidateTips = function(){ 
			initValidateTipsDiv($(this), 'tipsDiv_' + $(this).attr('id'));
			return startValidateTips($(this)) 
		};
	}
}

$(function(){
	$("input[type=text]").each(function(){
		$(this).validateTips();
	})
})

function errorCssJNice(field){
	var obj;
	if(typeof field == 'string'){
		obj = document.getElementById(field);
	}else if(typeof field == 'object'){
		obj = $(field)[0];
	}
	if(obj){
		if(obj.tagName == 'INPUT'){
			var parentObj = $(obj).parent();
			parentObj.addClass('jNiceInputInner2');
		}else if(obj.tagName == 'SELECT'){
			if(_isie6_){
				$(obj).parent().find('span.jNiceSelectText').addClass('jNiceSelectText2');;
			}else{
				$(obj).next('div').find('span.jNiceSelectText').addClass('jNiceSelectText2');;
			}
		}else if(obj.tagName == 'TEXTAREA'){
			$(obj).parent().append('<div class="textarea_errorbg">&nbsp;</div>');
		}
	}
}

function normalCssJNice(field){
	var obj;
	if(typeof field == 'string'){
		obj = document.getElementById(field);
	}else if(typeof field == 'object'){
		obj = $(field)[0];
	}
	if(obj){
		if(obj.tagName == 'INPUT'){
			var parentObj = $(obj).parent();
			parentObj.removeClass('jNiceInputInner2');
		}else if(obj.tagName == 'SELECT'){
			if(_isie6_){
				$(obj).parent().find('span.jNiceSelectText').removeClass('jNiceSelectText2');;
			}else{
				$(obj).next('div').find('span.jNiceSelectText').removeClass('jNiceSelectText2');;
			}
		}else if(obj.tagName == 'TEXTAREA'){
			$(obj).parent().find('div.textarea_errorbg').remove();
		}
	}
}

function errorCss(field){
	var obj;
	if(typeof field == 'string'){
		obj = document.getElementById(field);
	}else if(typeof field == 'object'){
		obj = $(field)[0];
	}
	if(obj){
		if(obj.tagName == 'INPUT'){
			$(obj).addClass('input_errorbg');
		}else if(obj.tagName == 'SELECT'){
			$(obj).parent().append('<div class="select_errorbg">&nbsp;</div>');
		}
	}
}

function normalCss(field){
	var obj;
	if(typeof field == 'string'){
		obj = document.getElementById(field);
	}else if(typeof field == 'object'){
		obj = $(field)[0];
	}
	if(obj){
		if(obj.tagName == 'INPUT'){
			$(obj).removeClass('input_errorbg');
		}else if(obj.tagName == 'SELECT'){
			$(obj).parent().find('div.select_errorbg').remove();
		}
	}
}










